package stru1.day11;

/**
 * 101. 对称二叉树
 */
public class Solution3 {
    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(1, new TreeNode(2, new TreeNode(3), new TreeNode(4)),
                new TreeNode(2, new TreeNode(4), new TreeNode(3)));
        System.out.println(isSymmetric(root1));

        TreeNode root2 = new TreeNode(1, new TreeNode(2, null, new TreeNode(3)),
                new TreeNode(2, null, new TreeNode(3)));
        System.out.println(isSymmetric(root2));
    }

    /**
     * 递归
     */
    public static boolean isSymmetric(TreeNode root) {
        return compare(root, root);
    }

    private static boolean compare(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;
        if (left == null || right == null) return false;
        return left.val == right.val && compare(left.left, right.right) && compare(left.right, right.left);
    }
}
